﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Admin_EditUser : System.Web.UI.Page
{
    UsersController usersController = new UsersController();
    PermissionController permissionController = new PermissionController();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ddlPermission.DataSource = permissionController.GetAll();
            ddlPermission.DataTextField = "name";
            ddlPermission.DataValueField = "permission_id";
            ddlPermission.DataBind();
            int id = Convert.ToInt32(Request.QueryString["id"]);
            if (Request.QueryString["id"]==null)
            {
                error();
                return;
            }
            if(!string.IsNullOrWhiteSpace(id.ToString()) && id!=null)
            {
                
                DataTable dt =usersController.GetByID(id);
                Users u = new Users(dt.Rows[0]);
                txtUsername.Text = u.Username;
                txtAddress.Text = u.Address;
                txtEmail.Text = u.Email;
                txtPhone.Text = u.Phone;
                txtBirthday.Text = u.Birthday.ToString("yyyy-MM-dd");
                txtProvince.Text = u.Province;
                txtDistrict.Text = u.District;
                ddlPermission.ClearSelection();
                ddlPermission.Items.FindByValue(u.Permission_id.ToString()).Selected = true;
                if(u.Status)
                {
                    ddlStatus.SelectedIndex = 0;
                 }
                else
                {
                    ddlStatus.SelectedIndex = 1;
                }

            }
            else
            {
                error();
            }
        }
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

    }
    protected void error()
    {
        Response.Redirect("~/Error.aspx");
    }
}